package com.ruoyi.framework.mybatisplus;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * MP注入处理器
 * @author Lion Li
 * @date 2021/4/25
 */
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {


	private void baseFill(MetaObject metaObject) {
		this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
		this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
		this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getLoginUser().getUser().getNickName()); // 起始版本 3.3.0(推荐使用)
		this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getLoginUser().getUser().getNickName()); // 起始版本 3.3.0(推荐使用)
	}

	@Override
	public void insertFill(MetaObject metaObject) {
		try {
			//补充创建人id,用于权限 --> 注意: 表结构设计原因 createById非通用BaseEntity属性
			boolean hasCreateById = metaObject.hasSetter("createById");
			if (hasCreateById) {
				this.strictInsertFill(metaObject, "createById",  Long.class,SecurityUtils.getUserId());
				baseFill(metaObject);
				return;
			}
			if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
				Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
						? baseEntity.getCreateTime() : new Date();
				baseEntity.setCreateTime(current);
				baseEntity.setUpdateTime(current);
				String username = StrUtil.isNotBlank(baseEntity.getCreateBy())
						? baseEntity.getCreateBy() : SecurityUtils.getLoginUser().getUser().getNickName();
				// 当前已登录 且 创建人为空 则填充
				baseEntity.setCreateBy(username);
				// 当前已登录 且 更新人为空 则填充
				baseEntity.setUpdateBy(username);
			} else if (ObjectUtil.isNotNull(metaObject)){
				baseFill(metaObject);
			}
		} catch (Exception e) {
			log.error("自动注入异常 =>" + e.getMessage());
		}
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		try {
			if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
				Date current = new Date();
				// 更新时间填充(不管为不为空)
				baseEntity.setUpdateTime(current);
				try {
					String username = SecurityUtils.getLoginUser() == null ? null : SecurityUtils.getLoginUser().getUser().getNickName();
					// 当前已登录 更新人填充(不管为不为空)
					if (StrUtil.isNotBlank(username)) {
						baseEntity.setUpdateBy(username);
					}
				} catch (Exception e) {
					log.error("未获取到token用户");
				}

			}
			else if (ObjectUtil.isNotNull(metaObject)){
				this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
				try {
					this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getLoginUser() == null ? null : SecurityUtils.getLoginUser().getUser().getNickName()); // 起始版本 3.3.0(推荐使用)
				} catch (Exception e){
					log.error("未获取到token用户");
				}
			}
		} catch (Exception e) {
			throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
		}
	}
}
